<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}">
    <link rel="stylesheet" th:href="@{/css/custom.form.css}">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div id="searchParam" class="layui-form layui-card-header my-card-header-auto" shiro:hasPermission="sys:log:list">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <input type="text" id="userId" class="layui-input" autocomplete="off" placeholder="用户ID">
                </div>
                <div class="layui-inline">
                    <input type="text" id="username" class="layui-input" autocomplete="off" placeholder="请输入账号">
                </div>
                <div class="layui-inline">
                    <input type="text" id="operation" class="layui-input" autocomplete="off" placeholder="用户操作动作">
                </div>
                <div class="layui-inline">
                    <input type="text" class="layui-input" id="createTime" autocomplete="off" placeholder="创建时间">
                </div>
                <div class="layui-inline ">
                    <button class="layui-btn" id="search">查询</button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <table class="layui-hide" id="log_table" lay-filter="log_table"></table>
            <div class="layui-table-page">
                <div id="laypage"></div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button type="button" class="layui-btn layui-btn-sm" lay-event="batchDelLog" shiro:hasPermission="sys:log:delete">
            <i class="layui-icon">&#xe640;</i> 批量删除
        </button>
    </div>
</script>
<script type="text/html" id="tool">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delLog" shiro:hasPermission="sys:log:delete">删除</a>
</script>
<script th:src="@{/layui/layui.js}"></script>
<script th:src="@{/js/core.util.js}"></script>
<script>
    layui.use(['table', 'laypage', 'layer', 'laydate', 'jquery'], function () {
        var table = layui.table;
        var laypage = layui.laypage;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var $ = layui.jquery;
        var searchParam = {
            pageNum: 1,
            pageSize: 10,
            username: null,
            userId: null,
            operation: null,
            startTime: null,
            endTime: null
        };
        var basePath = CoreUtil.getContextPath();
        CoreUtil.sendAjax(basePath + "/api/logs", JSON.stringify(searchParam), function (res) {
            //初始化分页插件
            laypageTable(res.data.totalRows, searchParam.pageNum);
            //异步加载表格数据
            if (res.data.list != null) {
                loadTable(res.data.list);
            }
        }, "POST", false, function (res) {
            //无权限访问操作日志列表提示
            layer.msg("抱歉！您暂无获取操作日志列表的权限");
            var noAuthorityData=[];
            loadTable(noAuthorityData);
        });

        //渲染分页插件
        var laypageTable = function (count, currentPage) {
            laypage.render({
                elem: 'laypage',
                count: count,
                limit: searchParam.pageSize,
                layout: ['prev', 'page', 'next', 'skip', 'limit', 'refresh', 'count'],
                curr: location.hash.replace('#!currentPage=', ''), //获取起始页
                hash: 'currentPage', //自定义hash值
                //连续出现的页码个数
                groups: 3,
                //自定义“上一页”的内容，支持传入普通文本和HTML
                prev: '<i class="layui-icon">&#xe603;</i>',
                //自定义“下一页”的内容，同上
                next: '<i class="layui-icon">&#xe602;</i>',
                jump: function (obj, first) {
                    if (!first) {
                        searchParam.pageNum = obj.curr;
                        searchParam.pageSize = obj.limit;
                        CoreUtil.sendAjax(basePath + "/api/logs", JSON.stringify(searchParam), function (res) {
                            if (res.data.list != null) {
                                loadTable(res.data.list);
                                laypageTable(res.data.totalRows, searchParam.pageNum);
                            }
                        }, "POST", false, function (res) {
                            //无权限访问操作日志列表提示
                            layer.msg("抱歉！您暂无获取操作日志列表的权限");
                            var noAuthorityData=[];
                            loadTable(noAuthorityData);
                        });
                    }
                }
            });
        };

        //渲染table
        var loadTable = function (data) {
            //展示已知数据
            table.render({
                elem: '#log_table',
                cols: [
                    [
                        {type: 'checkbox', fixed: 'left'},
                        {field: 'userId', title: '用户ID', width: 120, sort: true},
                        {field: 'username', title: '账号', width: 120},
                        {field: 'operation', title: '用户操作', width: 140},
                        {field: 'time', title: '耗时', width: 60},
                        {field: 'method', title: '请求方法', width: 200},
                        {field: 'params', title: '请求参数', width: 200},
                        {field: 'ip', title: '请求IP地址', width: 120},
                        {
                            field: 'createTime', title: '创建时间', minWidth: 120, templet: function (item) {
                                return CoreUtil.formattime(item.createTime);
                            }
                        },
                        {width: 150, title: '操作', toolbar: '#tool'}
                    ]
                ],
                data: data,
                //不启用隔行背景
                even: false,
                limit: data.length,
                limits: [10, 20, 30, 40, 50],
                toolbar: '#toolbar'
            });
        };

        //执行查询
        $("#search").click(function () {
            searchParam.userId = $("#userId").val();
            searchParam.operation = $("#operation").val();
            searchParam.username = $("#username").val();
            searchParam.pageNum = 1;
            CoreUtil.sendAjax(basePath + "/api/logs", JSON.stringify(searchParam), function (res) {
                //初始化分页器
                laypageTable(res.data.totalRows, searchParam.pageNum);
                //初始化渲染数据
                if (res.data.list != null) {
                    loadTable(res.data.list);
                }
            }, "POST", false, function (res) {
                //无权限访问操作日志列表提示
                layer.msg("抱歉！您暂无获取操作日志列表的权限");
                var noAuthorityData=[];
                loadTable(noAuthorityData);
            });
        });

        //日期范围
        laydate.render({
            elem: '#createTime',
            type: 'datetime',
            range: '~',
            done: function (value, date, endDate) {
                if (value != null && value !== "") {
                    searchParam.startTime = value.split("~")[0];
                    searchParam.endTime = value.split("~")[1];
                } else {
                    //清空时间的时候要清空以前选择的日期
                    searchParam.startTime = null;
                    searchParam.endTime = null;
                }
            }
        });


        //表头工具栏事件
        table.on('toolbar(log_table)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'batchDelLog':
                    var data = checkStatus.data;
                    if (data.length === 0) {
                        layer.msg("请选择要批量删除的日志");
                    } else {
                        var logIds = [];
                        $(data).each(function (index, item) {
                            logIds.push(item.id);
                        });
                        // layer.msg(JSON.stringify(logIds));
                        tipDialog(logIds);
                    }
                    break;
            }
        });

        //操作工具栏事件
        table.on('tool(log_table)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'delLog':
                    var logIds = [];
                    logIds.push(data.id);
                    // layer.msg(JSON.stringify(logIds));
                    tipDialog(logIds);
                    break;
            }
        });

        //删除前确认对话框
        var tipDialog = function (logIds) {
            layer.open({
                content: '确定要删除么',
                yes: function (index, layero) {
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendAjax(basePath + "/api/log", JSON.stringify(logIds), function (res) {
                        layer.msg(res.msg);
                        $(".layui-laypage-btn").click();
                    }, "DELETE", false, function (res) {
                        //无权限删除操作日志提示
                        layer.msg("抱歉！您暂无删除操作日志的权限");
                    });
                }
            });
        };

    });
</script>
</html>